<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\card\service;

class UserService
{
    private $_uniacid;
    public function initFirstUserToStaff($staff_id, $uniacid)
    {
        longbingGetCompanyConfig($uniacid);
        $count = \app\card\model\User::where(["uniacid" => $uniacid, "is_staff" => 1])->count();
        $result = false;
        if(!$count) {
            $result = $this->addUserToStaff($staff_id, [], $uniacid);
        }
        return $result;
    }
    public function addUserToStaff($staff_id, $companyIds, $uniacid)
    {
        $params = ["staff_id" => $staff_id, "company_ids" => $companyIds];
        $this->_uniacid = $uniacid;
        if(!isset($params["staff_id"]) || empty($params["staff_id"])) {
            return lang("not staff");
        }
        $user = longbingGetUser($params["staff_id"], $this->_uniacid);
        if(empty($user)) {
            return lang("not user");
        }
        $permissions = longbingGetPluginAuth($this->_uniacid);
        if(empty($user["is_staff"]) && !empty($permissions) && isset($permissions["card_number"]) && !empty($permissions["card_number"]) && longbingGetCardNum($this->_uniacid) >= $permissions["card_number"]) {
            return lang("not card num");
        }
        $result = true;
        if(empty($permissions) || !isset($permissions["plugin"]) || !isset($permissions["plugin"]["boss"]) || empty($permissions["plugin"]["boss"])) {
            $params["company_ids"] = [];
            $company_model = new \app\card\model\Company();
            $company = $company_model->getCompany(["uniacid" => $this->_uniacid, "status" => 1], ["id"]);
            if(!empty($company)) {
                $params["company_ids"][] = $company["id"];
            }
        } else {
            if(!isset($params["company_ids"]) || empty($params["company_ids"])) {
                return lang("not company id");
            }
            \app\card\model\CardBoss::where([["user_id", "=", $params["staff_id"]]])->delete();
            $boss = implode(",", $params["company_ids"]);
            $result = \app\card\model\CardBoss::create(["user_id" => $params["staff_id"], "boss" => $boss, "uniacid" => $this->_uniacid]);
        }
        if($result === false) {
            return "edit failed";
        }
        $check = true;
        if(empty($user["is_staff"])) {
            $user["is_staff"] = 1;
            longbingSetUser($params["staff_id"], $this->_uniacid, $user);
            $user_model = new \app\card\model\User();
            $check = $user_model->updateUser(["id" => $params["staff_id"], "uniacid" => $this->_uniacid], ["is_staff" => 1]);
        }
        if($check) {
            $staff = longbingGetUserInfo($params["staff_id"], $this->_uniacid);
            $staff_model = new \app\card\model\UserInfo();
            $company_id = "";
            if(isset($params["company_ids"][0]) && !empty($params["company_ids"][0])) {
                $company_id = $params["company_ids"][0];
            }
            if(empty($staff)) {
                $result = $staff_model->createUser(["fans_id" => $params["staff_id"], "uniacid" => $this->_uniacid, "is_staff" => 1, "company_id" => $company_id, "is_default" => 1]);
                longbingGetUserInfo($params["staff_id"], $this->_uniacid, true);
            }
            if(!empty($staff)) {
                $company_id = $staff["company_id"];
                if(isset($params["company_ids"]) && !empty($params["company_ids"]) && !in_array($staff["company_id"], $params["company_ids"])) {
                    $company_id = $params["company_ids"][0];
                }
                $result = $staff_model->updateUser(["fans_id" => $params["staff_id"], "uniacid" => $this->_uniacid], ["is_staff" => 1, "is_default" => 1, "company_id" => $company_id]);
                $staff["is_staff"] = 1;
                $staff["is_default"] = 1;
                $staff["company_id"] = $company_id;
                longbingSetUserInfo($params["staff_id"], $this->_uniacid, $staff);
            }
        }
        return $result;
    }
    public static function getDefaultStraffNumber($uniacid)
    {
        $count = \app\card\model\User::alias("a")->join("longbing_card_user_info b", "b.fans_id = a.id")->where("a.is_staff", 1)->where("b.is_staff", 1)->where("b.is_default", 1)->where("a.uniacid", $uniacid)->where("b.uniacid", $uniacid)->count();
        return $count;
    }
    public static function createHeaderQr($_uniacid, $params)
    {
        $staff = longbingGetUserInfo($params["staff_id"], $_uniacid);
        if(empty($staff) || empty($staff["is_staff"])) {
            return lang("staff info not found");
        }
        $data = ["data" => ["staff_id" => $params["staff_id"], "pid" => $params["staff_id"], "type" => 4, "key" => 1]];
        $result = longbingCreateWxCode($_uniacid, $data);
        if(isset($result["qr_path"])) {
            $result = transImagesOne($result, ["qr_path"], $_uniacid);
        }
        $config = longbingGetAppConfig($_uniacid);
        if(!empty($staff["avatar"]) && !empty($result) && !empty($result["qr_path"]) && !empty($result["path"]) && isset($config["qr_avatar_switch"]) && !empty($config["qr_avatar_switch"])) {
            $staff = transImagesOne($staff, ["avatar"], $_uniacid);
            $dat = longbingUpdateQrByAvatar($staff["avatar"], $result["qr_path"], $_uniacid, $result["path"]);
        }
        if(empty($result) || !isset($result["qr_path"])) {
            return lang("create qr error");
        }
        \app\card\model\User::update(["qr_path" => $result["qr_path"]], ["id" => $params["staff_id"]]);
        longbingGetUser($params["staff_id"], $_uniacid, true);
        return $result;
    }
    public static function getLastStaffId($_uniacid, $userId)
    {
        $where[] = ["uniacid", "=", $_uniacid];
        $where[] = ["id", "=", $userId];
        $last_staff_id = \app\card\model\User::where($where)->value("last_staff_id");
        return $last_staff_id;
    }
    public static function delUserInfoCache($_uniacid, $userId)
    {
        $key = "longbing_card_info_" . $userId;
        return delCache($key, $_uniacid);
    }
}

?>